package apii.servlet.prof;

import static apii.servlet.prof.ProfUtils.finishResponse;
import static apii.servlet.prof.ProfUtils.getSessionProf;
import static apii.servlet.prof.ProfUtils.redirectToException;
import static apii.servlet.prof.ProfUtils.redirectToLogin;
import static apii.servlet.prof.ProfUtils.startResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import apii.server.ServerThings;
import apii.servlet.ServletUtils;

/**
 * Servlet implementation class ViewLabServlet
 */
// тут k - primary key соответствующего предмета, i - primary key
// соответствующей лабы
// assigned by root address: /prof/subjects/subjk/labi
// assigned by web.xml address: /prof_view_lab?subject_id=k&lab_id=i
@WebServlet("/ViewLabServlet")
public class ViewLabServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ViewLabServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try (Connection c = ServerThings.getThings().getConnection();
				PreparedStatement groupsStatement = c.prepareStatement(
						"select groups.group_id, groups.name from groups, discipline_orders " +
						"where discipline_orders.group_id = groups.group_id " +
						"and discipline_orders.discipline_id = ?");
				PreparedStatement labStatement = c.prepareStatement(
						"select labs.name from labs where labs.lab_id = ?");) {
			ProfData profData = getSessionProf(c, request);
			if (profData == null) {
				redirectToLogin(response);
				return;
			}
			int discId = (Integer) request.getAttribute(ProfRootServlet.DISCIPLINE_ID);
			int labId = (Integer) request.getAttribute(ProfRootServlet.LAB_ID);
			groupsStatement.setInt(1, discId);
			labStatement.setInt(1, labId);
			ResultSet groupsRS = groupsStatement.executeQuery();
			ResultSet labRS = labStatement.executeQuery();
			labRS.next();
			
			// the response
			startResponse(response, "Лаба: " + labRS.getString(1), profData);
			PrintWriter pw = response.getWriter();
			pw.println("	<H1>Выберите группу.</H1>");
			pw.println("	<ul>");
			while (groupsRS.next()) {
				int groupId = groupsRS.getInt(1);
				String groupName = groupsRS.getString(2);
				pw.println("		<li><a href=\"" + ServletUtils.PREFIX
						+ "/prof/subjects/" + ServletUtils.getDisciplineURLElement(discId) + "/" +
						ServletUtils.getLabURLElement(labId) + "/" + ServletUtils.getGroupURLElement(groupId)
						+ "\">" + groupName + "</a></li>");
			}
			pw.println("	</ul>");
			finishResponse(response);
		} catch (SQLException e) {
			e.printStackTrace();
			redirectToException(response);
		}
	}
}
